import { TrackNameOpenBtn } from "./TrackNameOpenBtn";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { ObjCreate } from "../../../common/ObjCreate";
import { GlobalStyle } from "../../../../../common/global/GlobalStyle";
import { TaskProcessPanel } from "../../imgtxt/TaskProcessPanel";
import { TaskInfo } from "../../../../model/TaskInfo";
import { EffectManager } from "../../../../manager/EffectManager";
import { GameController } from "../../../../mvc/GameController";
import { ETaskStatus } from "../../../../../../Message/Game/ETaskStatus";
import { Global } from "../../../../../../com/gengine/global/Global";
import { HTMLUtil } from "../../../../../../com/gengine/utils/HTMLUtil";
import { FontUtil } from "../../../../../common/font/FontUtil";
import { ETaskGroup } from "../../../../../../Message/Game/ETaskGroup";
import { Language } from "../../../../../../extend/language/Language";
import { ColorConfig } from "../../../../resource/ColorConfig";
import { TaskUtil } from "../../TaskUtil";
import { Cache } from "../../../../cache/Cache";
import { ClockManager } from "../../../../manager/ClockManager";
import { SecTimer } from "../../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../../com/gengine/core/frame/TimerType";
import { RemainTimeTool } from "../../../award/RemainTimeTool";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../../events/DataEvent";
import { EventName } from "../../../../mvc/EventName";
import { GButton } from "../../../../../../com/mui/controls/GButton";
import { DisplayUtil } from "../../../../../common/DisplayUtil";
import { LineBox } from "../../../guide/LineBox";
type int = number;
//class TrackItem
    
    export  class TrackItem /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._openBtn = new TrackNameOpenBtn();
            this._openBtn.x = 4;
            this._openBtn.y = 4;
            this._openBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOpenBtnClickHandler);
            this._titleTxt = new Object /* flash.text.TextField */();
            this._titleTxt.selectable = false;
            this._titleTxt.textColor = 16777215;
            this._titleTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._titleTxt.filters = [FilterConst.nameGlowFilter];
            this._titleTxt.x = 20;
            addChild(this._titleTxt);
            this._bodySprite = new Object /* flash.display.Sprite */();
            this._bodySprite.mouseEnabled = false;
            this._bodySprite.x = 20;
            this._bodySprite.y = 20;
            addChild(this._bodySprite);
            this._bodyTxt = ObjCreate.createTextField("", 0, 0, -1, -1, null, GlobalStyle.getTextFormatDefaultFont(GlobalStyle.textFormat1));
            this._bodyTxt.textColor = 16777215;
            this._bodyTxt.selectable = false;
            this._bodyTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._bodyTxt.multiline = true;
            this._bodyTxt.wordWrap = true;
            this._bodyTxt.filters = [FilterConst.nameGlowFilter];
            this._bodyTxt.width = 214;
            this._bodyTxt.mouseWheelEnabled = false;
            this._bodySprite.addChild(this._bodyTxt);
            this._bodyImg = new TaskProcessPanel();
            this._bodyImg.x = 20;
            this._bodyImg.y = 20;
            addChild(this._bodyImg);
            this._titleTxt.addEventListener(flash.events.TextEvent.LINK, this.onTitleLinkHandler);
            return;
        }

        public get taskInfo(): TaskInfo
        {
            return this._task;
        }

        public getImgRect(): Object /* flash.geom.Rectangle */
        {
            return this._bodyImg.getImgRect();
        }

        public getTargetRect(arg1: string, arg2: Boolean=true): Object /* flash.geom.Rectangle */
        {
            if (arg2) 
            {
                return this._bodyImg.getTargetRect(arg1);
            }
            var loc1=this._bodyTxt.text.indexOf(arg1);
            var loc2;
            if ((loc2 = this._bodyTxt.getCharBoundaries(loc1)) == null) 
            {
                return this.getImgRect();
            }
            loc2.x = loc2.x + this._bodyTxt.x;
            loc2.y = loc2.y + (-(this._bodyTxt.scrollV - 1)) * this._bodyTxt.getLineMetrics(0).height;
            loc2.width = loc2.width * arg1.replace(" ", "").length;
            return loc2;
        }

        public get bodyImgX(): number
        {
            return this._bodyImg.x;
        }

        /* internal  */hideOptTxt(): void
        {
            if (this._optTxt) 
            {
                if (this._optTxt.parent) 
                {
                    this._optTxt.parent.removeChild(this._optTxt);
                }
            }
            return;
        }

        public get bodyImgY(): number
        {
            return this._bodyImg.y;
        }

        public get isClose(): Boolean
        {
            return this._openBtn.isClose;
        }

        public showOrHideInfo(): void
        {
            if (this._openBtn.isClose) 
            {
                this.openInfo();
            }
            else 
            {
                this.closeInfo();
            }
            return;
        }

        public openInfo(): void
        {
            this._openBtn.open();
            if (!this._bodySprite.parent) 
            {
                this.addChild(this._bodySprite);
            }
            if (!this._bodyImg.parent) 
            {
                this.addChild(this._bodyImg);
            }
            return;
        }

        public closeInfo(): void
        {
            this._openBtn.close();
            if (this._bodySprite.parent) 
            {
                this.removeChild(this._bodySprite);
            }
            if (this._bodyImg.parent) 
            {
                this.removeChild(this._bodyImg);
            }
            return;
        }

        public /* override */ get height(): number
        {
            if (this._openBtn.isClose) 
            {
                return 20;
            }
            var loc1=20 + this._bodyTxt.height;
            if (this._optBtn && this._optBtn.parent) 
            {
                loc1 = loc1 + (this._optBtn.height + 2);
            }
            else if (this._optTxt && this._optTxt.parent) 
            {
                loc1 = loc1 + this._optTxt.height;
            }
            return loc1;
        }

        public addLineBox(): void
        {
            var removeLineBox: Function;

            var loc1;
            removeLineBox = null;
            removeLineBox = (): void
            {
                EffectManager.disposeLineBox(_lineBox);
                _lineBox.dispose();
                return;
            }
            this._lineBox = EffectManager.getLineBox(this.width, this.height);
            this._lineBox.y = 2;
            this.addChildAt(this._lineBox, (this.numChildren - 1));
            flash.utils.setTimeout(removeLineBox, 5000);
            return;
        }

        /* internal  */onTitleLinkHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            var loc1=int(arg1.text);
            if (loc1 == 0) 
            {
                GameController.group.view.show();
            }
            return;
        }

        public initTask(arg1: TaskInfo): void
        {
            this.updateTask(arg1);
            this.openInfo();
            if (arg1.status != ETaskStatus._ETaskStatusCanGet) 
            {
                addChild(this._openBtn);
                this._titleTxt.x = 20;
                this._bodySprite.x = 20;
                this._bodyImg.x = 20;
            }
            else 
            {
                if (this._openBtn.parent) 
                {
                    removeChild(this._openBtn);
                }
                this._titleTxt.x = 0;
                this._bodySprite.x = 0;
                this._bodyImg.x = 0;
            }
            return;
        }

        public updateTask(arg1: TaskInfo): void
        {
            this._task = arg1;
            Global.instance.callLater(this.updataText);
            return;
        }

        /* internal  */updataText(): void
        {
            if (this._task == null) 
            {
                return;
            }
            this._titleTxt.htmlText = this._task.group + HTMLUtil.addColor(this._task.taskName, this._task.taskNameColor);
            this._bodyTxt.htmlText = HTMLUtil.addColor(this._task.processStr, "#ffffff", FontUtil.defaultFont.fontName);
            if (this._task.sTask.group != ETaskGroup._ETaskGroupCycle) 
            {
                if (this._task.sTask.group != ETaskGroup._ETaskGroupSchool) 
                {
                    this._titleTxt.htmlText = this._titleTxt.htmlText + this._task.isCompleted;
                }
                else if (this._task.status != ETaskStatus._ETaskStatusNotCompleted) 
                {
                    this._titleTxt.htmlText = this._titleTxt.htmlText + this._task.isCompleted;
                }
                else 
                {
                    this._titleTxt.htmlText = this._titleTxt.htmlText + ("<u><a href=\'event: 0\'>" + HTMLUtil.addColor("[" + Language.getString(20701) + "]", ColorConfig.instance.getTaskColor("status_com").color) + "</a></u>");
                }
                this.hideOptBtn();
                this.hideOptBtn2();
                if (this._task.status != ETaskStatus._ETaskStatusNotCompleted) 
                {
                    if (this._task.status == ETaskStatus._ETaskStatusHadCompleted && TaskUtil.isCanGetDoubleRewardTask(this._task.sTask)) 
                    {
                        this.showOptBtn2(Language.getString(80481));
                        this.showOptBtn(Language.getString(20700));
                    }
                    else if (this._task.status != ETaskStatus._ETaskStatusCanGet) 
                    {
                        if (this._task.isCanEndQuick && !(this._task.status == ETaskStatus._ETaskStatusWaitForCompleted)) 
                        {
                            this.showOptBtn(Language.getString(20700));
                        }
                    }
                }
                else if (this._task.isCanComQuick) 
                {
                    this.showOptBtn(Language.getString(20023));
                }
            }
            else 
            {
                this._titleTxt.htmlText = this._titleTxt.htmlText + this._task.isCompleted;
                if (this._task.status == ETaskStatus._ETaskStatusCanGet) 
                {
                    this.hideOptBtn();
                }
                else 
                {
                    this.showOptBtn(Language.getString(20700));
                }
            }
            if (Global.isDebugModle) 
            {
                this._titleTxt.htmlText = this._titleTxt.htmlText + ("-" + this._task.sTask.code);
            }
            if (Cache.instance.role.entityInfo.level >= 9) 
            {
                this._bodyImg.updateData(this._bodyTxt, this._task);
            }
            else 
            {
                this._bodyImg.updateData(this._bodyTxt, this._task, true);
            }
            if (this._task.sTask.group != ETaskGroup._ETaskGroupKillEvil) 
            {
                if (this._task.sTask.group == ETaskGroup._ETaskGroupLove) 
                {
                    if (this._task.status != ETaskStatus._ETaskStatusNotCompleted) 
                    {
                        this.stopTimer();
                    }
                    else 
                    {
                        this.startTimer(this._task.loveTaskEndTime);
                    }
                }
            }
            else if (this._task.status != ETaskStatus._ETaskStatusNotCompleted) 
            {
                this.stopTimer();
            }
            else 
            {
                this.startTimer(this._task.createDt.time + 3600 * 2000);
            }
            this.resetBtnPosition();
            this.resetTxtPosition();
            return;
        }

        /* internal  */startTimer(arg1: number): void
        {
            var loc1=(arg1 - ClockManager.instance.nowDate.time) / 1000;
            if (loc1 <= 0) 
            {
                return;
            }
            if (loc1 > 7200) 
            {
                loc1 = 7200;
            }
            if (!this._timer) 
            {
                this._timer = new SecTimer();
                this._timer.addListener(TimerType.ENTERFRAME, this.onTimerEnterFrameHandler);
            }
            this._timer.delay = 1;
            this._timer.repeatCount = loc1;
            if (!this._timer.running) 
            {
                this._timer.start();
            }
            if (this._task.sTask.group != ETaskGroup._ETaskGroupLove) 
            {
                this.showOptTxt(HTMLUtil.addColor(RemainTimeTool.getRemainTimeBySeconds(this._timer.repeatCount), "#ff0000"));
            }
            else 
            {
                this.showOptTxt(HTMLUtil.addColor(Language.getString(21223), "#00ff00") + HTMLUtil.addColor(RemainTimeTool.getRemainTimeBySeconds(this._timer.repeatCount), "#ff0000"));
            }
            return;
        }

        /* internal  */stopTimer(): void
        {
            if (this._timer && this._timer.running) 
            {
                this._timer.stop();
                this._timer.isDelete = false;
            }
            this.hideOptTxt();
            return;
        }

        /* internal  */onTimerEnterFrameHandler(arg1: SecTimer): void
        {
            var loc1=arg1.repeatCount % 2 != 0 ? "#00ff00" : "#ff0000";
            if (this._task.sTask.group != ETaskGroup._ETaskGroupLove) 
            {
                this.showOptTxt(HTMLUtil.addColor(RemainTimeTool.getRemainTimeBySeconds(arg1.repeatCount), loc1));
            }
            else 
            {
                this.showOptTxt(HTMLUtil.addColor(Language.getString(21223), "#00ff00") + HTMLUtil.addColor(RemainTimeTool.getRemainTimeBySeconds(this._timer.repeatCount), loc1));
            }
            return;
        }

        /* internal  */showOptTxt(arg1: string): void
        {
            if (!this._optTxt) 
            {
                this._optTxt = new Object /* flash.text.TextField */();
                this._optTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
                this._optTxt.selectable = false;
                this._optTxt.width = 100;
                this._optTxt.height = 20;
                this._optTxt.filters = [FilterConst.nameGlowFilter];
            }
            this._optTxt.htmlText = arg1;
            if (!this._bodySprite.contains(this._optTxt)) 
            {
                this._bodySprite.addChild(this._optTxt);
                this.resetTxtPosition();
            }
            return;
        }

        /* internal  */onOpenBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this.taskInfo.status == ETaskStatus._ETaskStatusCanGet) 
            {
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TrackInfoShowOrHide, this));
            return;
        }

        /* internal  */showOptBtn(arg1: string): void
        {
            if (!this._optBtn) 
            {
                this._optBtn = new GButton();
                this._optBtn.setSize(60, 22);
                this._optBtn.styleName = "ButtonMini";
            }
            this._optBtn.label = arg1;
            this._optBtn.textField.filters = [FilterConst.nameGlowFilter];
            this._optBtn.x = 10;
            this._optBtn.y = this._bodyTxt.height;
            this._optBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOptBtnClickHandler);
            this._bodySprite.addChild(this._optBtn);
            return;
        }

        /* internal  */showOptBtn2(arg1: string): void
        {
            if (!this._optBtn2) 
            {
                this._optBtn2 = new GButton();
                this._optBtn2.setSize(60, 22);
                this._optBtn2.styleName = "ButtonMini";
            }
            this._optBtn2.label = arg1;
            this._optBtn2.textField.filters = [FilterConst.nameGlowFilter];
            this._optBtn2.x = 75;
            this._optBtn2.y = this._bodyTxt.height;
            this._optBtn2.addEventListener(flash.events.MouseEvent.CLICK, this.onOptBtnClickHandler);
            this._bodySprite.addChild(this._optBtn2);
            return;
        }

        /* internal  */hideOptBtn(): void
        {
            if (this._optBtn) 
            {
                DisplayUtil.removeMe(this._optBtn);
                this._optBtn.removeEventListener(flash.events.MouseEvent.CLICK, this.onOptBtnClickHandler);
            }
            return;
        }

        /* internal  */hideOptBtn2(): void
        {
            if (this._optBtn2) 
            {
                DisplayUtil.removeMe(this._optBtn2);
                this._optBtn2.removeEventListener(flash.events.MouseEvent.CLICK, this.onOptBtnClickHandler);
            }
            return;
        }

        /* internal  */resetBtnPosition(): void
        {
            if (this._optBtn && this._optBtn.parent) 
            {
                this._optBtn.x = 10;
                this._optBtn.y = this._bodyTxt.height;
            }
            return;
        }

        /* internal  */resetTxtPosition(): void
        {
            if (this._optTxt && this._optTxt.parent) 
            {
                if (this._optBtn && this._optBtn.parent) 
                {
                    this._optTxt.x = this._optBtn.x + this._optBtn.width + 10;
                    this._optTxt.y = this._bodyTxt.height + 2;
                }
                else 
                {
                    if (this._task && this._task.sTask.group == ETaskGroup._ETaskGroupLove) 
                    {
                        this._optTxt.x = 0;
                    }
                    else 
                    {
                        this._optTxt.x = 10;
                    }
                    this._optTxt.y = this._bodyTxt.height;
                }
            }
            return;
        }

        /* internal  */onOptBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as GButton;
            if (loc1 == null) 
            {
                return;
            }
            if (loc1.label != Language.getString(20023)) 
            {
                if (loc1.label != Language.getString(20700)) 
                {
                    if (loc1.label == Language.getString(80481)) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.TaskRewardDoubleReq, this._task));
                    }
                }
                else 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickEndReq, this._task));
                }
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickComReq, this._task));
            }
            return;
        }

        public dispose(): void
        {
            this._task = null;
            this._bodyImg.dispose();
            if (this._timer) 
            {
                this._timer.dispose();
                this._timer = null;
            }
            this.hideOptBtn();
            this.hideOptTxt();
            return;
        }

        public get taskId(): int
        {
            if (this._task) 
            {
                return this._task.sTask.code;
            }
            return 0;
        }

        /* internal */ /* var */_openBtn: TrackNameOpenBtn;

        /* internal */ /* var */_titleTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_bodySprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_bodyTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_task: TaskInfo;

        /* internal */ /* var */_timer: SecTimer;

        /* internal */ /* var */_optBtn: GButton;

        /* internal */ /* var */_optBtn2: GButton;

        /* internal */ /* var */_optTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_lineBox: LineBox;

        /* internal */ /* var */_bodyImg: TaskProcessPanel;
    }
